<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        管理
        <small>列表</small>
    </h1>
</section>

<!-- Main content -->
<section class="content" id="speaking-app">
    <div class="row">
        <div class="col-md-8">
            <a class="btn btn-default" @click="getList">刷新</a>
            <a v-show="!show.edit" class="btn btn-info" @click="add">添加</a>
            <a v-show="show.edit" class="btn btn-danger" @click="show.edit = false">关闭</a>
        </div>
    </div>

    <div class="row" v-show="!show.edit">
        <div class="col-md-12">
            <table class="table table-responsive table-striped table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>老师</th>
                    <th>标题</th>
                    <th>时间</th>
                    <th>语音</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="item in list">
                    <td>{{item.id}}</td>
                    <td>{{item.teacher_id | Teacher}}</td>
                    <td>{{item.title}}</td>
                    <td>{{item.created | TimeFmt}}</td>
                    <td><audio :src="item.url" controls></audio></td>
                    <td>
                        <a class="btn btn-info btn-xs" @click="edit(item)">编辑</a>
                        <a class="btn btn-danger btn-xs" @click="del(item)">删除</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="100">
                        <pagination :total="page.total" :limit="page.limit" :offset="page.offset" @turn="turnPage" ></pagination>
                    </td>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row" v-show="show.edit">
        <div class="col-md-6 col-md-offset-2">
            <h3>编辑</h3>
            <div class="form" role="form">
                <div class="form-group" :class="{'has-warning': !edit_item.id}">
                    <label>老师：</label>
                    <select class="form-control" v-model="edit_item.teacher_id">
                        <option v-for="t in teachers" :value="t.id">{{t.realname}}</option>
                    </select>
                </div>
                <div class="form-group ">
                    <label>标题(50字)：</label>
                    <input class="form-control" v-model="edit_item.title" maxlength="50">
                </div>
                <div class="form-group">
                    <label>语音文件：</label>
                    <file-uploader v-model="edit_item.url" filetype="audio" oss="true" policyUrl="/alioss/get_policy" filesize="20M" @duration="setDuration"></file-uploader>
                </div>

                <div class="form-group">
                    <a class="btn btn-primary" @click="save">发表</a>
                    <a class="btn btn-default" @click="show.edit = false">关闭</a>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->

<script>
    var speaking_app = new Vue({
        el: '#speaking-app',
        data: {
            list: [],
            edit_item: {},
            page: {
                total: 0,
                limit: 10,
                offset: 0
            },
            teachers: [],
            show: {
                edit: false
            }
        },
        created: function () {
            this.getList();
            ajax({
                uri: '/common/teacher_names',
                success: function (resp) {
                    this.teachers = resp.data;
                }.bind(this)
            })
        },
        methods: {
            getList: function () {
                ajax({
                    uri: '/speaking/list',
                    data: this.page,
                    success: function(resp){
                        this.list = resp.list;
                        this.page.total = resp.total;
                    }.bind(this)
                });
            },
            add: function () {
                this.edit_item = {};
                this.show.edit = true;
            },
            edit: function (item) {
                this.edit_item = item;
                this.show.edit = true;
            },
            save: function () {
                var item = this.edit_item;
                if (!item.teacher_id || !item.title || !item.url) {
                    alert('请完善资料！');
                    return;
                }

                ajax({
                    uri: '/speaking/save',
                    data: item,
                    success: function(resp){
                        this.getList();
                        this.show.edit = false;
                    }.bind(this)
                });
            },
            del: function (item) {
                ajax({
                    uri: '/speaking/del',
                    data: {id: item.id},
                    success: function(resp){
                        this.getList();
                    }.bind(this)
                });
            },
            turnPage: function (offset) {
                this.page.offset = offset;
            },
            setDuration: function (duration) {
                this.edit_item.duration = duration;
            }
        },
        filters: {
            /**
             * @return {string}
             */
            Teacher: function (id) {
                var teachers = speaking_app.teachers;
                for (var i=0; i<teachers.length; i++) {
                    var t = teachers[i];
                    if (t.id == id) {
                        return t.realname;
                    }
                }
                return '';
            },
            /**
             * @return {string}
             */
            TimeFmt: function (time) {
                if (typeof time === "string") {
                    return time.substr(0, 19);
                }
                return '';
            }
        },
        watch: {
            'page.offset': function (nv, ov) {
                this.getList();
            }
        }
    })
</script>